'use strict';
const Service = require('egg').Service;
const qiniu = require('qiniu');
const accessKey = 'v2tylBA2Kup8wZW3ys9g9gvsVm52m4MJ3GgOWC1B';
const secretKey = 'paabZ8JD-MaAm9afMrF493U8Tmx8rAfdP_yy0Dtr';
const publicBucketDomain = 'http://qiniu.fengweio.com';
const options = {
  scope: 'instagram',
  expires: 7200,
};

class QiniuService extends Service {
  async getQiniuToken() {
    const { ctx } = this;
    if (!accessKey || !secretKey || !publicBucketDomain) {
      ctx.throw(400, '请配置七牛鉴权参数');
    }

    const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
    const putPolicy = new qiniu.rs.PutPolicy(options);
    const uploadToken = putPolicy.uploadToken(mac);
    return uploadToken;
  }
}

module.exports = QiniuService;
